package cn.micro.keep.accounts.service.impl;

import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.entity.AccountOdsRecord;
import cn.micro.keep.accounts.entity.AccountOdsRecordImgRelation;
import cn.micro.keep.accounts.entity.dto.AccountOdsRecordDTO;
import cn.micro.keep.accounts.entity.dto.AccountOdsRecordSearch;
import cn.micro.keep.accounts.entity.dto.PieChartSearch;
import cn.micro.keep.accounts.entity.vo.AccountOdsRecordInfoVO;
import cn.micro.keep.accounts.entity.vo.AccountOdsRecordVO;
import cn.micro.keep.accounts.entity.vo.PieChartVO;
import cn.micro.keep.accounts.entity.vo.YesterdayStatisticsVO;
import cn.micro.keep.accounts.mapper.AccountOdsRecordImgRelationMapper;
import cn.micro.keep.accounts.mapper.AccountOdsRecordMapper;
import cn.micro.keep.accounts.mapstruct.AccountOdsRecordMapstruct;
import cn.micro.keep.accounts.service.AccountOdsRecordService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 账单信息表 服务实现类
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-08
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AccountOdsRecordServiceImpl extends ServiceImpl<AccountOdsRecordMapper, AccountOdsRecord> implements AccountOdsRecordService {

    private final AccountOdsRecordImgRelationMapper accountOdsRecordImgRelationMapper;

    @Override
    @DSTransactional
    public boolean create(AccountOdsRecordDTO dto) {
        AccountOdsRecord accountOdsRecord = new AccountOdsRecord();
        AccountOdsRecordMapstruct.INSTANCE.dtoToEntity(dto,accountOdsRecord);
        accountOdsRecord.setUserId(SecurityUtil.getUserId());
        if (this.save(accountOdsRecord)) {
            List<Long> imgIds = dto.getImgIds();
            if (imgIds!= null && imgIds.size() > 0) {
                List<AccountOdsRecordImgRelation> relations = new ArrayList<>();
                for (Long imgId : imgIds) {
                    AccountOdsRecordImgRelation accountOdsRecordImgRelation = new AccountOdsRecordImgRelation();
                    accountOdsRecordImgRelation.setRecordId(accountOdsRecord.getId());
                    accountOdsRecordImgRelation.setImgId(imgId);
                    relations.add(accountOdsRecordImgRelation);
                }
                accountOdsRecordImgRelationMapper.insertBatch(relations);
            }
            return true;
        }
        return false;
    }

    @Override
    @DSTransactional
    public boolean deleteRecord(Long id) {
        if (this.removeById(id)) {
            LambdaQueryWrapper<AccountOdsRecordImgRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AccountOdsRecordImgRelation::getRecordId,id);
            accountOdsRecordImgRelationMapper.delete(queryWrapper);
            return true;
        }
        return false;
    }

    @Override
    public List<AccountOdsRecordVO> listByUser(AccountOdsRecordSearch search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        return this.baseMapper.queryByUser(search);
    }

    @Override
    public AccountOdsRecordInfoVO info(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public YesterdayStatisticsVO yesterdayStatistics() {
        //获取当前的用户ID

        return null;
    }

    @Override
    public List<PieChartVO> pieChart(PieChartSearch search) {
        search.setUserId(SecurityUtil.getUserId());
        return this.baseMapper.queryPieChart(search);
    }
}
